﻿@using FujiyBlog.Core.DomainObjects
@using FujiyBlog.Web.Common
@using FujiyBlog.Core.Extensions

@model IEnumerable<FujiyBlog.Core.DomainObjects.User>
@{
    Layout = MVC.Admin.User.Views._Layout;
    ViewBag.Title = Resources.Labels.Users;
}

<h2>@Resources.Labels.Users</h2>

@if (User.IsInRole(Role.CreateNewUsers))
{
    <p>
        @Html.ActionLink("Create New User", "Create")
    </p>
}

<table>
    <tr>
        <th>
            Username
        </th>
        <th>
            Email
        </th>
        <th>
            DisplayName
        </th>
        <th>
            FullName
        </th>
        <th>
            CreationDate
        </th>
        <th>
            LastLoginDate
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr id="item-row-@item.Id">
            <td>
                @Html.DisplayFor(modelItem => item.Username)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DisplayName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FullName)
            </td>
            <td>
                @DateTimeUtil.ConvertUtcToMyTimeZone(item.CreationDate)
            </td>
            <td>
                @DateTimeUtil.ConvertUtcToMyTimeZone(item.LastLoginDate)
            </td>
            <td>
            @if ((item.Username != User.Identity.Name && User.IsInRole(Role.EditOtherUsers)) ||
                (item.Username == User.Identity.Name && User.IsInRole(Role.EditOwnUser)))
            {
                <text>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                
                <span id="disable-link-@item.Id"@if(!item.Enabled){<text> style="display:none"</text>}>
                    @Ajax.ActionLink("Disable", MVC.Admin.User.Disable(item.Id), new AjaxOptions { HttpMethod = "POST", Confirm = "Sure?", OnSuccess = "AfterDisable(arguments[0], " + item.Id + ");" })
                </span>
                <span id="enable-link-@item.Id"@if(item.Enabled){<text> style="display:none"</text>}>
                    @Ajax.ActionLink("Enable", MVC.Admin.User.Enable(item.Id), new AjaxOptions { HttpMethod = "POST", Confirm = "Sure?", OnSuccess = "AfterEnable(arguments[0], " + item.Id + ");" })
                </span>
                </text>
            }
            </td>
        </tr>
    }
</table>
<script type="text/javascript">
    function AfterDisable(response, itemId) {
        if (!response.errorMessage) {
            $('#disable-link-' + itemId).hide();
            $('#enable-link-' + itemId).show();
        }
        else {
            alert(response.errorMessage);
        }
    }

    function AfterEnable(response, itemId) {
        if (!response.errorMessage) {
            $('#disable-link-' + itemId).show();
            $('#enable-link-' + itemId).hide();
        }
        else {
            alert(response.errorMessage);
        }
    }
</script>
